package com.hooker.mail;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;

public class MailInfo {
    private String mailServerHost;// 发送邮件的服务器的IP
    private String mailServerPort;// 发送邮件的服务器的端口
    private String fromAddress;// 邮件发送者的地址
    private Set<String> toAddress = new HashSet<>();   // 邮件接收者的地址
    private String userName;// 登陆邮件发送服务器的用户名
    private String password;// 登陆邮件发送服务器的密码
    private boolean validate = true;// 是否需要身份验证
    private String subject;// 邮件主题
    private String content;// 邮件的文本内容
    private List<File> attachFile;// 邮件附件

    /**
     * 获得邮件会话属性
     */
    public Properties getProperties() {
        Properties p = new Properties();
        p.put("mail.smtp.host", this.mailServerHost);
        p.put("mail.smtp.port", this.mailServerPort);
        p.put("mail.smtp.auth", validate ? "true" : "false");
        p.put("mail.smtp.ssl.enable", false);
//        p.put("mail.smtp.socketFactory.port", "465");
//        p.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//        p.setProperty("mail.smtp.socketFactory.fallback", "false");
        return p;
    }

    public String getMailServerHost() {
        return mailServerHost;
    }

    public void setMailServerHost(String mailServerHost) {
        this.mailServerHost = mailServerHost;
    }

    public String getMailServerPort() {
        return mailServerPort;
    }

    public void setMailServerPort(String mailServerPort) {
        this.mailServerPort = mailServerPort;
    }

    public boolean isValidate() {
        return validate;
    }

    public void setValidate(boolean validate) {
        this.validate = validate;
    }

    public List<File> getAttachFile() {
        return attachFile;
    }

    public void setAttachFile(List<File> fileNames) {
        this.attachFile = fileNames;
    }

    public String getFromAddress() {
        return fromAddress;
    }

    public void setFromAddress(String fromAddress) {
        this.fromAddress = fromAddress;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Set<String> getToAddress() {
        return toAddress;
    }

    public void addToAddress(String toAddress) {
        this.toAddress.add(toAddress);
    }

    public void setToAddress(Set<String> toAddress) {
        this.toAddress = toAddress;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String textContent) {
        this.content = textContent;
    }

    public static class Builder {
        private String mailServerHost;// 发送邮件的服务器的IP
        private String mailServerPort;// 发送邮件的服务器的端口
        private String fromAddress;// 邮件发送者的地址
        private Set<String> toAddress = new HashSet<>();   // 邮件接收者的地址
        private String userName;// 登陆邮件发送服务器的用户名
        private String password;// 登陆邮件发送服务器的密码
        private boolean validate = true;// 是否需要身份验证
        private String subject;// 邮件主题
        private String content;// 邮件的文本内容
        private List<File> attachFile;// 邮件附件的文件名

        public Builder setMailServerHost(String mailServerHost) {
            this.mailServerHost = mailServerHost;
            return this;
        }

        public Builder setMailServerPort(String mailServerPort) {
            this.mailServerPort = mailServerPort;
            return this;
        }

        public Builder setFromAddress(String fromAddress) {
            this.fromAddress = fromAddress;
            return this;
        }

        public Builder setToAddress(Set<String> toAddress) {
            this.toAddress = toAddress;
            return this;
        }

        public Builder setUserName(String userName) {
            this.userName = userName;
            return this;
        }

        public Builder setPassword(String password) {
            this.password = password;
            return this;
        }

        public Builder setValidate(boolean validate) {
            this.validate = validate;
            return this;
        }

        public Builder setSubject(String subject) {
            this.subject = subject;
            return this;
        }

        public Builder setContent(String content) {
            this.content = content;
            return this;
        }

        public Builder setAttachFileNames(List<File> attach) {
            this.attachFile = attach;
            return this;
        }

        public MailInfo build() {
            if (toAddress == null || toAddress.size() == 0) {
                throw new RuntimeException("toAddress can not be empty");
            }

            MailInfo info = new MailInfo();
            info.setMailServerHost(mailServerHost);
            info.setMailServerPort(mailServerPort);

            info.setFromAddress(fromAddress);
            info.setToAddress(toAddress);

            info.setUserName(userName);
            info.setPassword(password);
            info.setValidate(validate);
            info.setSubject(subject);
            info.setContent(content);
            info.setAttachFile(attachFile);
            return info;
        }
    }
}